package tree.nodeType.Boolean.functions;

import java.util.ArrayList;
import java.util.Random;

import pools.PoolsManager;
import tree.Node;
import tree.TreeNodesManager;
import tree.nodeType.Numeric;
import tree.nodeType.Boolean.BooleanNumericSonsFunction;
/**
 *small operator, has 2 numeric son s
 *
 */
public class SmallOperator extends BooleanNumericSonsFunction{

	public SmallOperator(Node parent, int depth, int maxDepth, TreeNodesManager treeNodesManager) {
		super(2, parent,treeNodesManager,depth);	
		initSons(depth,maxDepth,treeNodesManager);
	}
	
	public SmallOperator() {}
	
	@Override
	public void initSons(int depth, int maximumDepth, TreeNodesManager treeNodesManager) {
		for (int i=0;i<sonsNeeded;i++){
			if (!hasConstSon && random.nextDouble()>0.5){				
				sons.add(PoolsManager.get().getRandomConstantNode(this,treeNodesManager,depth+1));
				hasConstSon=true;
			}else{
				sons.add(PoolsManager.get().getRandomNumericFuncNode(depth+1, maximumDepth, this, treeNodesManager));
			}
		}
	}

	@Override
	public boolean execute() {
		
		return ((Numeric)sons.get(0)).execute()<((Numeric)sons.get(1)).execute();
	}


	@Override
	public String toString() {
		return "< operator";
	}
	
	@Override
	protected Node clone(TreeNodesManager treeNodesManager,Node parent) {
		Node cloned= new SmallOperator(this,treeNodesManager,parent);		
		return cloned;
	}
	
	@Override
	protected Node clone(Node parent) {
		Node cloned= new SmallOperator(this,parent);		
		return cloned;
	}
	
	public SmallOperator(SmallOperator smallOp,Node parent){
		this.depth=smallOp.depth;
		this.parent=parent;
		this.sonsNeeded=smallOp.sonsNeeded;
		this.random= new Random();
		this.sons= new ArrayList<Node>();
		for (Node son : smallOp.sons){
			this.sons.add(son.cloneSubTree(this));
		}
	}
	public SmallOperator(SmallOperator smallOp, TreeNodesManager treeNodesManager,Node parent){
		super(2,parent,treeNodesManager,smallOp.depth);		
		for (Node son : smallOp.sons){
			this.sons.add(son.cloneSubTree(treeNodesManager,this));
		}
	}
}
